<!-- BEGIN FOOTER -->
<div class="footer">
    <div class="container">
        <div class="row">
            <!-- BEGIN COPYRIGHT -->
            <div class="col-md-6 col-sm-6 text-left footer-text-left">
                2019 © ASA. ALL Rights Reserved.
            </div>
            <!-- END COPYRIGHT -->

            <!-- BEGIN POWERED -->
            <div class="col-md-6 col-sm-6 text-right">
                <p class="powered">Powered by: <a href="http://www.keenthemes.com/">KeenThemes.com</a></p>
            </div>
            <!-- END POWERED -->
        </div>
    </div>
</div>
<!-- END FOOTER -->

<!-- START PAGE LEVEL JAVASCRIPTS -->
<script src="/shop/assets/plugins/jquery.min.js"></script>
<script src="/shop/assets/plugins/jquery-migrate.min.js"></script>
<script src="/shop/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="/shop/assets/corporate/scripts/back-to-top.js"></script>
<script src="/shop/assets/plugins/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/shop/assets/plugins/fancybox/source/jquery.fancybox.pack.js"></script>
<script src="/shop/assets/plugins/owl.carousel/owl.carousel.min.js"></script>
<script src="/shop/assets/plugins/zoom/jquery.zoom.min.js"></script>
<script src="/shop/assets/plugins/bootstrap-touchspin/bootstrap.touchspin.js"></script>
<script src="/shop/assets/plugins/uniform/jquery.uniform.min.js"></script>
<script src="/shop/assets/plugins/rateit/src/jquery.rateit.js"></script>
<script src="/shop/assets/corporate/scripts/layout.js"></script>
<script src="/shop/assets/custom/bootstrap-submenu.js"></script>
<script src="/shop/assets/static/js/layer_mobile/layer.js"></script>
<script src="/shop/assets/custom/script.js"></script>
<!-- END PAGE LEVEL JAVASCRIPTS -->

<script type="text/javascript">
    /**
     * 判断函数是否存在
     * @param funcName 函数名
     */
    function isExitsFunction(funcName) {
        try {
            if (typeof (eval(funcName)) == "function") {
                return true;
            }
        } catch (e) {
        }
        return false;
    }

    /**
     * ajax提交封装函数
     * @param url ajax处理的url地址
     * @param data ajax数据，对象
     * @param callback 成功后的回调
     * @param locationUrl 页面执行成功后跳转的url地址，如果不填写则默认是当前页面的地址
     * @param type 默认是post提交
     */
    function ajax(url, data = {}, locationUrl = window.location.href, callback = null, type = 'post') {
        // 定义layer弹出层index
        var index = null;
        $.ajax({
            type: type,
            url: url,
            async: true,
            dataType: 'json',
            data: data,
            beforeSend: function () {
                // 定义一个loading
                index = layer.open({type: 2});
            },
            success: function (result) {
                // 关闭loading
                layer.close(index);
                // 判断是否有错误
                if (result.messages.length > 0) {
                    layer.open({
                        content: result.messages[0],
                        btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>"
                    });
                } else {
                    //提示
                    layer.open({
                        content: "<?php echo $this->obj->getValidateMessage('success'); ?>",
                        btn: "<?php echo $this->obj->getValidateMessage('please-wait'); ?>",
                        time: 3, // 3秒后自动跳转
                    });

                    // 这里执行成功后的回调逻辑
                    if (callback === null) {
                        // 如果为空，则默认跳转
                        setTimeout(function () {
                            window.location.href = locationUrl;
                        }, 1000);
                    } else {
                        // 否则就执行回调逻辑，把返回的result传进去
                        // 为了防止报错，先要判断callback是否存在
                        if (isExitsFunction(callback)) {
                            return callback(result);
                        }
                    }
                }
            },
            error: function () {
                // 错误也要给出提示
                layer.open({
                    content: "<?php echo $this->obj->getValidateMessage('system-error'); ?>",
                    btn: "<?php echo $this->obj->getValidateMessage('please-wait'); ?>",
                    time: 3, // 3秒后自动跳转
                });
            }
        });
        // 禁止自动跳转
        return false;
    }


    /**
     * 登录
     */
    $('#btn-login').click(function () {
        // 逻辑
        ajax('/login/login', {
            'login_name': $("#login_name").val(),
            'password': $("#password").val(),
            'language': $("#language").val(),
        });
    });


    /**
     * 删除指定商品的购物车
     * @param obj 当前节点
     * @param id 当前购物车的对应id
     */
    function delheadergoods(obj, id) {
        // 取出ul节点
        var li = $(obj).parent();
        var ul = $('.scroller');
        // 逻辑
        $.post('/buycar/deletecar/' + id, function (response) {
            // 判断是否成功
            if (response.messages.length > 0) {
                // 有错误信息，失败了
                layer.open({
                    content: result.messages[0],
                    btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>"
                });
            } else {
                // 没有错误信息，成功
                // 直接删除原来的节点即可，同时添加动画效果
                li.fadeTo("slow", 0.01, function () {
                    $(this).slideUp("slow", function () {
                        // 执行删除
                        $(this).remove();
                        // 监控是否还有li节点，如果不存在则直接跳转到空购物车页面
                        if (ul.children('li[class="buycar-list"]').length === 0) {
                            window.location.href = '/buycar';
                        }
                    });
                });
            }
        }, 'json');
    }


    /**
     * 用户主动取消指定id的订单
     * @param id
     */
    function cancle_order(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('confirm-cancle-order'); ?>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // ajax提交
                ajax('/order/cancle/' + id + '/1', {}, '/order/list');
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 用户针对指定id的订单申请退款
     * @param id
     */
    function refund_order(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('input-refund-reason'); ?><br/><br/><input type='text' name='reason' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // 判断退款理由是否填写了
                let reason_obj = $("input[name='reason']");
                if (reason_obj.val() === '' || reason_obj.val() === "<?php echo $this->obj->getValidateMessage('input-refund-reason'); ?>") {
                    //提示
                    reason_obj.val("<?php echo $this->obj->getValidateMessage('input-refund-reason'); ?>");
                    return false;
                }
                // 退款理由
                let reason = reason_obj.val();
                // ajax取消订单
                ajax('/order/applyrefund/' + id, {
                    "reason": reason
                });
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 管理员同意退款
     * @param id 订单id
     */
    function refund_agree(id) {
        // 逻辑
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('confirm-refund-agree'); ?>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // ajax提交
                ajax('/order/refundagree/' + id);
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 管理员拒绝退款
     * @param id 订单id
     */
    function refund_disagree(id) {
        // 逻辑
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('input-refund-disagree-reason'); ?><br/><br/><input type='text' name='reason' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // 判断拒绝退款理由是否填写了
                let reason_obj = $("input[name='reason']");
                if (reason_obj.val() === '' || reason_obj.val() === "<?php echo $this->obj->getValidateMessage('input-refund-reason'); ?>") {
                    //提示
                    reason_obj.val("<?php echo $this->obj->getValidateMessage('input-refund-disagree-reason'); ?>");
                    return false;
                }
                // 退款理由赋值
                let reason = reason_obj.val();
                // ajax提交
                ajax('/order/refunddisagree/' + id, {
                    "reason": reason
                });
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 添加地址
     * @returns {boolean}
     */
    function address_add() {
        // 逻辑
        ajax('/memberaddress/add', {
            'username': $('#username').val(),
            'mobile': $('#mobile').val(),
            'address': $('#address').val(),
        }, '/memberaddress');
    }


    /**
     * 地址设置为默认
     * @returns {boolean}
     */
    function address_set_default(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('confirm-default-adress'); ?>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // ajax提交
                ajax('/memberaddress/setdefault/' + id);
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 删除地址
     * @returns {boolean}
     */
    function address_del(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('confirm-delete-adress'); ?>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // ajax提交
                ajax('/memberaddress/del/' + id);
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 加入购物车函数
     * @param obj 当前节点
     * @param id 操作id
     * @returns {boolean}
     */
    function addbuycar(obj, id) {
        // 首先拿到所有的尺码input值
        var objParent = $(obj).parent().parent();
        var inputParent = objParent.next().next();
        var inputs = inputParent.find('input');
        // 保存最后的参数数组
        var sizecontentids = [];
        // 遍历获取值
        inputs.each(function () {
            sizecontentids[$(this).attr('data-id')] = $(this).val();
        });
        // ajax提交
        ajax('/buycar/add', {
            'sizecontentids': sizecontentids,
            'product_id': id,
        }, '/buycar');
    }


    /**
     * 邀请注册
     * @returns {boolean}
     */
    function invite() {
        // ajax提交
        ajax('/member/invite', {
            'email': $('#email').val(),
            'username': $('#username').val(),
            'companyid': $('#companyid').val(),
            'is_lockstock': $("input[name='is_lockstock']:checked").val(),
        });
    }


    // 更改订单截止时间
    function updatetime() {
        // ajax提交
        ajax('/order/updatetime', {
            'id': $('#modal-id').val(),
            'expire_time': $('input[name="modal_expire_time"]').val()
        });
    }

    /**
     * 查看订单详情
     * @param id 订单id
     */
    function order_view(id) {
        window.location.href = '/order/detail/' + id;
    }


    /**
     * 重置密码
     * @returns {boolean}
     */
    function reset_password() {
        // ajax提交
        ajax('/member/resetpassword', {
            'old_password': $('#old-password').val(),
            'new_password': $('#new-password').val(),
            'repeat_password': $('#repeat-password').val(),
        });
    }


    /**
     * 修改地址
     * @returns {boolean}
     */
    function address_edit(id) {
        // ajax提交
        ajax("/memberaddress/edit/" + id, {
            'username': $('#username').val(),
            'mobile': $('#mobile').val(),
            'address': $('#address').val()
        }, '/memberaddress');
    }


    /**
     * 注册新用户
     * @returns {boolean}
     */
    function reg() {
        // ajax提交
        ajax("/reg" + id, {
            'name': $('#name').val(),
            'password': $('#password').val(),
            'repeat_password': $('#repeat-password').val(),
            'email': $('#email').val(),
            'c': $('#c').val(),
        }, '/login');
    }


    /**
     * 物流发货
     * @param id
     */
    function ship_order(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('input-logistics-info'); ?><br/><br/><?php echo $this->obj->getValidateMessage('express_company'); ?>：<input type='text' name='express_company' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'><br/><br/><?php echo $this->obj->getValidateMessage('express_no'); ?>：<input type='text' name='express_no' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // 判断物流信息是否填全了
                let express_company = $("input[name='express_company']");
                let express_no = $("input[name='express_no']");
                if (express_company.val() === '' || express_company.val() === "<?php echo $this->obj->getValidateMessage('input'); ?>") {
                    //提示
                    express_company.val("<?php echo $this->obj->getValidateMessage('input'); ?>");
                    return false;
                }
                if (express_no.val() === '' || express_no.val() === "<?php echo $this->obj->getValidateMessage('input'); ?>") {
                    //提示
                    express_no.val("<?php echo $this->obj->getValidateMessage('input'); ?>");
                    return false;
                }
                // ajax提交逻辑
                ajax('/order/ship/' + id, {
                    "express_company": express_company.val(),
                    "express_no": express_no.val(),
                });
                // 关闭弹出层
                layer.close(index);
            }
        });
    }


    /**
     * 确认收货
     * @returns {boolean}
     */
    function receive_order(id) {
        // 询问框
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('confirm-receive-order'); ?>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // ajax提交
                ajax('/order/receive/' + id);
                // 关闭弹出层
                layer.close(index);
            }
        });
    }

    /**
     * 取出当前订单
     * @param {string} order_id 用户微信openid
     * @return {bool}
     */
    function getOrder(order_id) {
        // 初始化
        var result = false;
        // 同步处理
        $.ajaxSetup({async: false});
        // 逻辑
        $.post('/order/detail/' + order_id, {}, function (response) {
            // 判断逻辑
            if (response.messages.length === 0) {
                // 如果没有错误，返回模型
                result = response.data;
            } else {
                // 否则返回假
                result = false;
            }
        }, 'json');
        // 返回
        return result;
    }

    /**
     * 微信支付授权，给当前登录用户授权
     */
    function wechatAuth() {
        // 逻辑
        // 使用post请求
        $.post('/member/getwechatauth', {}, function (response) {
            if (response.messages.length !== 0) {
                $msg = response.messages[0];
            } else {
                $msg = response.data;
            }
            // 调出弹窗
            layer.open({
                content: $msg,
                btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>"
            });
        }, 'json');
    }

    /**
     * @param id
     * 微信支付授权，给当前登录用户授权
     */
    function wechatAuthById(id) {
        // 逻辑
        $.post('/member/getwechatauthbyid/' + id, {}, function (response) {
            if (response.messages.length !== 0) {
                // 询问框
                wechatauthbyidAction(id);
            } else {
                // 询问框
                layer.open({
                    content: response.data,
                    btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
                    yes: function (index) {
                        // 询问框
                        wechatauthbyidAction(id);
                        // 关闭弹出层
                        layer.close(index);
                    }
                });
            }
        }, 'json');
    }

    /**
     * 微信授权动作
     */
    function wechatauthAction() {
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('input-submchid'); ?><br/><br/><input type='text' name='sub_mch_id' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // sub_mch_id
                let obj = $("input[name='sub_mch_id']");
                let sub_mch_id = obj.val();
                // ajax取消订单
                ajax('/member/wechatauth', {
                    "sub_mch_id": sub_mch_id
                });
                // 关闭弹出层
                layer.close(index);
            }
        });
    }

    /**
     * 给指定的用户进行微信支付授权动作
     * @param id
     */
    function wechatauthbyidAction(id) {
        layer.open({
            content: "<?php echo $this->obj->getValidateMessage('input-submchid'); ?><br/><br/><input type='text' name='sub_mch_id' style='width:60%;height:30px;line-height:30px;padding:6px 10px;'>",
            btn: ["<?php echo $this->obj->getValidateMessage('yes'); ?>", "<?php echo $this->obj->getValidateMessage('no'); ?>"],
            yes: function (index) {
                // sub_mch_id
                let obj = $("input[name='sub_mch_id']");
                let sub_mch_id = obj.val();
                // ajax取消订单
                ajax('/member/wechatauthbyid/' + id, {
                    "sub_mch_id": sub_mch_id
                });
                // 关闭弹出层
                layer.close(index);
            }
        });
    }

    /**
     * 支付宝授权成功提示
     */
    function alipayAuth() {
        // 逻辑
        // 使用post请求
        $.post('/member/getalipayauth', {}, function (response) {
            layer.open({
                content: response.data,
                btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>"
            });
        }, 'json');
    }


    /**
     * 支付宝扫码支付
     * @param order_id 订单id
     * @returns {boolean}
     */
    function paymentByAliapy(order_id) {
        // 先判断远程页面返回的是json还是图片
        $.get('/alipay/pay/' + order_id, function (response, textStatus, jqXHR) {
            // 如果返回不是图片，则说明出错了，这个时候直接显示错误即可
            if (jqXHR.getResponseHeader('Content-Type') !== 'image/png') {
                // 弹出错误
                layer.open({
                    content: response,
                    btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>",
                });
            } else {
                // 否则就是成功了，取出返回的图片
                // 动态添加付款二维码
                let img = '<div id="qrcode"><p><?php echo $this->obj->getValidateMessage('scan-alipay-code-to-pay'); ?>：</p><img alt="二维码" src="/alipay/pay/' + order_id + '"></div>';
                // 弹出一张二维码图片
                layer.open({
                    content: img,
                    shadeClose: false,
                    btn: "<?php echo $this->obj->getValidateMessage('order-completed'); ?>",
                    yes: function () {
                        // 如果执行了完成按钮，则刷新页面
                        window.location.reload();
                    }
                });
            }
        });
    }

    /**
     * 微信扫码支付
     * @param order_id 订单id
     * @returns {boolean}
     */
    function paymentByWechat(order_id) {
        // 先判断远程页面返回的是json还是图片
        $.get('/wechatpay/pay/' + order_id, function (response, textStatus, jqXHR) {
            // 如果返回不是图片，则说明出错了，这个时候直接显示错误即可
            if (jqXHR.getResponseHeader('Content-Type') !== 'image/png') {
                // 弹出错误
                layer.open({
                    content: response,
                    btn: "<?php echo $this->obj->getValidateMessage('iknow'); ?>",
                });
            } else {
                // 否则就是成功了，取出返回的图片
                // 动态添加付款二维码
                let img = '<div id="qrcode"><p><?php echo $this->obj->getValidateMessage('scan-wechat-code-to-pay'); ?>：</p><img alt="二维码" src="/wechatpay/pay/' + order_id + '"></div>';
                // 弹出一张二维码图片
                layer.open({
                    content: img,
                    shadeClose: false,
                    btn: "<?php echo $this->obj->getValidateMessage('order-completed'); ?>",
                    yes: function () {
                        // 如果执行了完成按钮，则刷新页面
                        window.location.reload();
                    }
                });
            }
        });
    }
</script>
<!-- END PAGE LEVEL JAVASCRIPTS -->



